package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    public OrderService orderService;
    @Reference
    public OrderSettingService orderSettingService;

    /**
     * Excel文件上传， 并解析文件内容保存到数据库
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettings.add(orderSetting);
                }
            }
            orderSettingService.add(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//yyyy-MM
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据指定日期修改可预约人数
     *
     * @param orderSetting
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {//yyyy-MM
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    //预约分页查询
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        PageResult pageResult = orderSettingService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除预约
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderSettingService.delete(id);
        } catch (Exception e) {

        }
        return new Result(true, "删除成功");
    }

    //批量删除预约
    @RequestMapping("/deletemany")
    public Result deletemany(@RequestBody List<Map> list) {
        try {
            for (Map map : list) {
                Integer id = (Integer) map.get("id");
                orderSettingService.delete(id);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");

        }
        return new Result(true, "删除成功");
    }

    //根据预约日期查询是否可以预约
    @RequestMapping("/queryOrdersetting")
    public Result queryOrdersetting(@RequestBody Order order) {
        Date orderDate = order.getOrderDate();
        Result result = orderSettingService.findByOrderDate(orderDate);
        return result;
    }
    //新增预约
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer setmealId) throws Exception {
        Result result = orderService.add(map, setmealId);
        return result;
    }

    //确认预约
    @RequestMapping("/confirmOrder")
    public Result confirmOrder(Integer id,String telephone){
        return orderService.confirmOrder(id,telephone);
    }
}
